package cn.sharp.bbs.controller.admin;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.entity.BO.UserQuery;
import cn.sharp.bbs.service.DataDictService;
import cn.sharp.bbs.service.UserManageService;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/admin/user")
public class UserManageController {

    @Resource
    UserManageService userManageService;
    @Resource
    DataDictService dataDictService;

    /**
     * 获取用户详情
     * @param uid 用户id
     * @return 用户详细信息
     */
    @GetMapping("/{uid}/details")
    public Result getUserDetail(@PathVariable Long uid){
        if(userManageService.isInvalidUser(uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户id无效");
        }
        return Result.success(userManageService.getUserDetails(uid));
    }

    /**
     * 依据给出的查询信息生成用户列表
     * @param query 查询语句
     * @return 分页的用户列表
     */
    @GetMapping("/list")
    public Result getUserBriefList(@Nullable UserQuery query){
        if(query==null) query=new UserQuery();
        return Result.success(userManageService.getUserList(query));
    }

    /**
     * 更改用户状态
     * @param uid 要更改的用户id
     * @param state 要改为的状态名
     * @return 更改结果
     */
    @PostMapping("/{uid}/state")
    public Result editUserState(@RequestAttribute("CurrentUser") CurrentUser currentUser, @PathVariable("uid") Long uid, int state){
        if(Objects.equals(currentUser.getU_id(), uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "不能修改自己的状态!");
        }
        if(userManageService.isInvalidUser(uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户id无效");
        }
        if(!dataDictService.hasCodeWithType("user_state", state)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户状态无效");
        }
        userManageService.editUserState(uid, state);
        return Result.success();
    }

    /**
     * 获取用户角色列表
     * @param uid 用户id
     * @return 该用户的角色列表
     */
    @GetMapping("/{uid}/role")
    public Result getUserRole(@PathVariable("uid") Long uid){
        if(userManageService.isInvalidUser(uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户id无效");
        }
        return Result.success(userManageService.listUserRole(uid));
    }


    /**
     * 修改用户角色
     * @return 修改结果
     */
    @PostMapping("/{uid}/role/modify")
    public Result modifyUserRole(
            @RequestAttribute("CurrentUser") CurrentUser currentUser,
            @PathVariable("uid") Long uid,
            @RequestBody Long[] roles
    ){
        if(Objects.equals(currentUser.getU_id(), uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "不能修改自己的角色!");
        }
        if(userManageService.isInvalidUser(uid)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "用户id无效");
        }
        if(!userManageService.checkRoles(roles)){
            return Result.fail(Result.ERR_CODE_BUSINESS, "传入了无效的角色id");
        }
        userManageService.modifyUserRole(uid, roles);
        return Result.success();
    }

    @GetMapping("/getUserNum")
    public Result getUserNum(){
        return Result.success(userManageService.getUserNum());
    }

    @GetMapping("/getPostNum")
    public  Result getPostNum(){
        return Result.success(userManageService.getPostNum());
    }
}
